<?php
require('lib/verify.php');
require('lib/has_char.php');
require('lib/is_approved.php');

$_category = "wardkp";
$_title = "Item";
require('lib/head.php');

$item_id = $_GET["id"];
$system_id = $system["system_id"];

$sql = "select *
        from wm_bosses, wm_items 
        where wm_items.item_id = $item_id and 
              wm_items.item_boss = wm_bosses.boss_id";
$items = $db->query($sql);
$item = $db->fetch_assoc($items);

$sql = "select *, wm_characters.* 
        from wm_wishes, wm_characters 
        where wish_item = $item_id and  
              wish_system_id = $system_id and 
              wm_wishes.wish_character = wm_characters.character_id and 
              wm_characters.character_hidden = 0 order by wish_level desc";
$result = $db->query($sql);

boxTop("wardkp: $_title / " . $item["item_name"]);

show_item($item, $character, $db, "$war_http/item-view.php?id=" . $item_id, $system);

echo "<br /><br /><b>DKP Cost</b><br />";

$sql = "select * from wm_item_values where item_id = '" . $item["item_id"] . "' and item_system_id = $system_id";
$ress = $db->query($sql);
$ival = $db->fetch_assoc($ress);
if($ival["item_value"]==0) {
    echo '<i>Unknown cost; item never seen/paid for.</i>';
} else {
     echo number_format($ival["item_value"], 2).' dkp';
}
echo "<br />\n<br />\n\n<b>Effective Bids</b> <br />";

# Start listing bids
$bids = array();
$off_bids = array();
$bid_result = array();
while($ite = $db->fetch_assoc($result)) {
    $bid = $ite["character_dkp"] * bid2perc($ite["wish_level"]);
    if($bid < ($floor_max)) {
        $bid = $floor_max;
    }
    if($ite["wish_level"] == 1) {
        # $debids[$ite["character_id"]] = $bid;
        # $dbid_result[$ite["character_id"]] = $ite;
        $off_bids[$ite["character_id"]] = $bid;
        $bid_result[$ite["character_id"]] = $ite;
    } else {
        $bids[$ite["character_id"]] = $bid;
        $bid_result[$ite["character_id"]] = $ite;
    }
 }
arsort($bids);
arsort($off_bids);

while(list($n, $ite) = each($bids)) {
    $ite = $bid_result[$n];
    echo '<b>';
    echo number_format($bids[$ite["character_id"]], 2).'</b> dkp ';
    echo bid2text($ite["wish_level"]).' by ';
    echo cclass($ite["character_class"]);
    echo ' <a href="character.php?id='.$ite["wish_character"].'">'.$ite["character_name"].'</a> (';
    echo number_format($ite["character_dkp"], 2).' dkp)<br />';
 }

// micro bids:
echo "<br>Off spec bids:<br>";
while(list($n, $ite) = @each($off_bids)) {
    $ite = $bid_result[$n];
    echo '<b>'.number_format($ite["character_dkp"], 2).'</b> dkp by ';
    echo cclass($ite["character_class"]);
    echo ' <a href="character.php?id='.$ite["wish_character"].'">'.$ite["character_name"].'</a><br />';
 }
?>

<br />
<b>Looted by</b><br />
<?php
$lit = $item["item_id"];
$sql = "select *, wm_characters.*, wm_credits.*, wm_instances.*, wm_raids.*, wm_bosses.*, wm_item_values.* 
        from wm_loots 
             left join wm_characters on wm_characters.character_id = loot_character 
             left join item_cache on wm_loots.loot_item = item_cache.item_id 
             left join wm_credits on wm_credits.credit_id = wm_loots.loot_credit 
             left join wm_bosses on wm_bosses.boss_id = wm_credits.credit_boss 
             left join wm_raids on wm_raids.raid_id = wm_credits.credit_raid 
             left join wm_instances on wm_instances.instance_id = wm_bosses.boss_instance 
             left join wm_item_values on wm_item_values.item_id = wm_loots.loot_item 
        where wm_loots.loot_item = $lit and wm_credits.credit_system_id = $system_id 
        order by wm_credits.credit_date desc";
$result = $db->query($sql);

echo '<i>Looted '.$db->num_rows($result).' times.</i><br />';

while($loot = $db->fetch_assoc($result)) {
    echo '<a href="character.php?id='.$loot["loot_character"].'">'.$loot["character_name"].'</a> ';
    if ($loot["loot_level"] == 1) {
        echo bid2text($loot["loot_level"]).'<br />';
    } else {
        echo bid2text($loot["loot_level"]).' ('.number_format($loot["loot_bid"], 2).' bid) ('.number_format($loot["item_value"], 2).' paid)<br />';
    }
    echo '<a href="credit.php?id='.$loot["credit_id"].'">'.$loot["boss_name"].', '.date("g:i a", strtotime($loot["credit_date"])).'</a><br />';
    echo '<a href="raids.php?id='.$loot["raid_id"].'">'.$loot["instance_name"].', '.date("D d M Y", strtotime($loot["raid_date"])).'</a><br/>';
    echo '<br />';
    // TODO: credit.php doesn't exist.
 }

boxBot();

require('lib/foot.php');
?>

